package com.example.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.mapper.SongMapper;
import com.example.model.Song;
import com.example.utils.ResultVOUtil;

@RestController
@RequestMapping("/api/song")
public class SongController {

	@Resource
	private SongMapper songMapper;

	@GetMapping("/list")
	public Object list() {
		List<Song> songList = songMapper.findAll();
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("songList", songList);
		return ResultVOUtil.success(resultMap);
	}

	// // 添加或修改
	@PostMapping("/add")
	public Object add(Song song) {
		System.out.println("保存歌名:" + song.getName());
		System.out.println("保存歌手:" + song.getSinger());
		
		song.setCreatetime(new Date().getTime());
		song.setUpdatetime(new Date().getTime());
		songMapper.save(song);
		return ResultVOUtil.success();
	}

	@PostMapping("/update")
	public Object update(Song song) {
		System.out.println("修改歌名:" + song.getName());
		System.out.println("修改歌手:" + song.getSinger());
		
		song.setUpdatetime(new Date().getTime());
		songMapper.updatSongById(song);
		return ResultVOUtil.success();
	}

	// 删除
	@PostMapping("/delete")
	public Object delete(@RequestParam("id") int id) {
		System.out.println("删除歌曲的id: "+id);
		songMapper.delete(id);
		return ResultVOUtil.success();
	}

	// 根据id获取详细
	@GetMapping("/getDetail")
	public Object getDetail(@RequestParam("id") int id) {
		Song song = songMapper.selectSongById(id);
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("song", song);
		return ResultVOUtil.success(resultMap);
	}

}
